﻿using Blog.Core.Domain.Model.User;
using Blog.Core.DTO;
using Blog.Core.DTO.ViewModel.User;
using Blog.Core.IServices;
using Blog.Core.IServices.IUser;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Blog.Core.Api.Controllers
{
    /// <summary>
    /// 权限控制器
    /// </summary>
    [Authorize]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PermissionsController : BaseController<Permissions>
    {
        private readonly IPermissionsService permissionsService;
        private readonly IRolesPermissionService _rolePermissionService;
        /// <summary>
        /// 权限控制器 增删改查
        /// </summary>
        /// <param name="baseService"></param>
        public PermissionsController(IBaseService<Permissions> baseService, IPermissionsService permissionsService, IRolesPermissionService rolePermissionService) : base(baseService)
        {
            this.permissionsService = permissionsService;
            _rolePermissionService = rolePermissionService;
        }
        /// <summary>
        /// 树形格式显示权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<IActionResult> GetPermission(int id = 0)
        {
            ResponseResultDTO<List<ResponsePermissionTreeDTO>> result = new ResponseResultDTO<List<ResponsePermissionTreeDTO>>();
            result.Code = ResultStatusCode.SUCCESS;
            result.Message = "成功";
            result.Data = await permissionsService.GetPermission(id);
            return Ok(result);
        }
        /// <summary>
        /// 树形方式显示用于列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPermissionList(int id = 0)
        {
            ResponseResultDTO<List<ResponsePermissionListDTO>> result = new ResponseResultDTO<List<ResponsePermissionListDTO>>();
            result.Code = ResultStatusCode.SUCCESS;
            result.Message = "成功";
            result.Data = await permissionsService.GetPermissionList(id);
            return Ok(result);
        }
        /// <summary>
        /// 角色权限添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RolePermission(RequestRolePermissionDTO entity)
        {
            ResponseResultDTO<dynamic> result = new ResponseResultDTO<dynamic>();
            if (ModelState.IsValid)
            {
                int h = await _rolePermissionService.RolePermission(entity);
                if (h > 0)
                {
                    result.Code = ResultStatusCode.SUCCESS;
                    result.Message = "操作成功";
                    return Ok(result);
                }
                else
                {
                    result.Code = ResultStatusCode.PARAMSERROR;
                    result.Message = "参数错误";
                    return Ok(result);
                }

            }
            else
            {
                result.Code = ResultStatusCode.PARAMSERROR;
                result.Message = "参数错误";
                return Ok(result);
            }
        }
        /// <summary>
        /// 角色权限的反填
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<dynamic> GetRolePermission(int roleid)
        {
            return Ok(await _rolePermissionService.GetRolePermission(roleid));
        }
        /// <summary>
        /// 角色权限修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateRolePermission(RequestRolePermissionDTO entity)
        {
            ResponseResultDTO<dynamic> result = new ResponseResultDTO<dynamic>();

            //模型验证
            if (ModelState.IsValid)
            {
               return Ok( await _rolePermissionService.UpdatePermission(entity));
            }
            else
            {
                result.Code = ResultStatusCode.PARAMSERROR;
                result.Message = "参数错误";
                return Ok(result);
            }
        }
    }
}
